package com.i_art_engineer.meigong;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.Locale;

/**
 * 客户端HTTP访问
 */
public class AppHttpClient {

    public final static String UA_HOST = "i-art-engineer.COM";
    public static final String DELETE = "DELETE";
    public static final String GET = "GET";
    public static final String POST = "POST";
    public static final String PUT = "PUT";
    public static final String HTTP_HEADER_ACCESS_TICKET = "x-access-ticket";

    public static AsyncHttpClient client;

    static {

    }

    private AppHttpClient() {
    }

    public static AsyncHttpClient getHttpClient() {
        return client;
    }

    public static void cancelAll(Context context) {
        client.cancelRequests(context, true);
    }

    public static void clearUserCookies(Context context) {
        // (new HttpClientCookieStore(context)).a();
    }

    public static void addHeader(String header, String tokenValue){
        client.addHeader(header, tokenValue);
    }

    public static void get(String urlString,JsonHttpResponseHandler res)   //不带参数，获取json对象或者数组
    {
        client.get(urlString, res);
    }

    public static void delete(String url, AsyncHttpResponseHandler handler) {
        client.delete(url, handler);
        log(new StringBuilder("DELETE ").append(url).toString());
    }

    public static void get(String url, AsyncHttpResponseHandler handler) {
        client.get(url, handler);
        log(new StringBuilder("GET ").append(url).toString());
    }

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler handler) {
        client.get(url, params, handler);
        log(new StringBuilder("GET ").append(url).append("&").append(params).toString());
    }

    public static void log(String log) {
        AppLogger.v("BaseApi", log);
    }

    public static void post(String url, AsyncHttpResponseHandler handler) {
        log(new StringBuilder("POST ").append(url).toString());
        client.post(url, handler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler handler) {
        log(new StringBuilder("POST ").append(url).append("&").append(params).toString());
        client.post(url, params, handler);
    }

    public static void put(String url, AsyncHttpResponseHandler handler) {
        client.put(url, handler);
        log(new StringBuilder("PUT ").append(url).toString());
    }

    public static void put(String url, RequestParams params, AsyncHttpResponseHandler handler) {
        client.put(url, params, handler);
        log(new StringBuilder("PUT ").append(url).append("&").append(params).toString());
    }

    public static void setUserAgent(String userAgent) {
        client.setUserAgent(userAgent);
    }

    public static String getUserAgent() {
        StringBuilder ua = new StringBuilder(UA_HOST);
        ua.append('/' + AppContext.instance().getPackageInfo().versionName + '_'
                + AppContext.instance().getPackageInfo().versionCode);
        ua.append("/Android");// 手机系统平台
        ua.append("/" + android.os.Build.VERSION.RELEASE);// 手机系统版本
        ua.append("/" + android.os.Build.MODEL); // 手机型号
        ua.append("/" + AppContext.instance().getAppId());// 客户端唯一标识
        return ua.toString();
    }

    public static void setTicket(String ticket) {
        client.addHeader(HTTP_HEADER_ACCESS_TICKET, ticket);
    }

    public static void cleanTicket() {
        client.removeHeader(HTTP_HEADER_ACCESS_TICKET);
    }

    public static void setHttpClient(AsyncHttpClient c) {
        client = c;
        client.addHeader("Accept-Language", Locale.getDefault().toString());
        //client.addHeader("Connection", "Keep-Alive");
        //client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
        setUserAgent(getUserAgent());
    }

}
